/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.City;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 *
 * @author Admin
 */
@ManagedBean(name = "Cities")
@SessionScoped
public class CityController {

    CityHelper helper;
    DataModel lstCities;
    private City newCity;
    private City selectedCity;
    public CityController() {
        helper = new CityHelper();
        newCity = new City();
       // selectedCity = new City();
    }
    
    public void changeSelected(City c){
        selectedCity = c;
    }
    
    public void stateChangeListener(ValueChangeEvent event) {
        selectedCity = (City)event.getNewValue();
    }
    
    public DataModel listCities(){
        lstCities =  new ListDataModel(helper.getAllCity());
        return lstCities;
    }
    
    public List<City> listCities2(){
        return helper.getAllCity();
    }
    public void addCity(){
        this.helper.updateCity(getNewCity());
    }
    
    public void editCity(City c){
        this.helper.updateCity(c);
    }
    
    public void deleteCity(City c){
        this.helper.deleteCity(c);
    }

    /**
     * @return the newCity
     */
    public City getNewCity() {
        return newCity;
    }

    /**
     * @param newCity the newCity to set
     */
    public void setNewCity(City newCity) {
        this.newCity = newCity;
    }

    /**
     * @return the selectedCity
     */
    public City getSelectedCity() {
        return selectedCity;
    }

    /**
     * @param selectedCity the selectedCity to set
     */
    public void setSelectedCity(City selectedCity) {
        this.selectedCity = selectedCity;
    }
}
